package com.yinkgh.mybatis.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yinkgh.mybatis.common.controller.BaseController;
import com.yinkgh.mybatis.model.Employee;
import com.yinkgh.mybatis.model.JsonModel;
import com.yinkgh.mybatis.model.Operation;
import com.yinkgh.mybatis.model.User;
import com.yinkgh.mybatis.service.UserService;
import com.yinkgh.mybatis.view.UserView;
import com.yinkgh.util.BeanCopyUtil;

@Controller
@RequestMapping("/user")
public class UserController implements BaseController<UserView> {

	@Autowired
	private UserService userService;
//	private UserMapper userMapper;
	
	@RequestMapping(value = "/userIndex", produces = "application/json;charset=UTF-8")
    public String userList(HttpServletRequest request, ModelMap modelMap) {

//		DataSourceContextHolder.setDbType(DataSourceType.DATASOURCEB);
		//		List<User> userList = userMapper.selectUserList();
		List<User> userList = userService.selectListByPrimaryKey(new User());
		modelMap.put("userList", userList);

		System.out.println("====>>查找userList<<=====");
		
		return "user/userlist";
	}
	
	@RequestMapping("/toAddUser")
	public String toAddUser(UserView userView) {
		return "user/addUser";
	}
	
	@RequestMapping(value="/insertUser", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String doInsert() {
		String result = "插入成功";
		User record = new User();
		Employee employee = new Employee();
		try {
			record.setPassword("2234");
			record.setUsername("wangwu");
			
			employee.setEmail("1111");
			employee.setMobile(123456L);
			employee.setName("1222333");
			userService.insert(record,  employee);
		} catch (Exception e) {
			e.printStackTrace();
			result = "插入失败";
		} 
		return result;
	}
	
	@RequestMapping("/addUser")
	@ResponseBody
	@Override
	public String doAdd(UserView userView) {
//		System.out.println(userView);
		String result = "result=";
		int flagB = userService.insertB(userView);
		int flagA = userService.insertA(userView);
		if (flagB > 0) {
			result += "flagB success";
		}
		if (flagA > 0) {
			result += "flagA success";
		}
		return result;
	}

	@RequestMapping("/deleteUser")
	@ResponseBody
	@Override
	public String doDelete(Long id) {
		
		return "deleteUser";
	}

	@RequestMapping("/updateUser")
	@ResponseBody
	@Override
	public String doUpdate(UserView t) {
		// TODO Auto-generated method stub
		return "updateUser";
	}
	
	/**
	 * 测试出来fastjson的一个bug
	 * @return
	 */
	@RequestMapping("/jsonTest")
	@ResponseBody
	public Map<String, Object> jsonTest() {
		Map<String, Object> result = new HashMap<String, Object>();
		JsonModel jsonModel = new JsonModel();
		
		List<Operation> operations = new ArrayList<Operation>();
		
		Operation operation = new Operation();
		operation.setOperationDesc("操作描述");
		operation.setOperationName("操作名称");
		
		operations.add(operation);
		operations.add(operation);
		operations.add(operation);
		operations.add(operation);
		
		jsonModel.setDesc("描述");
		jsonModel.setTitle("json样例");
		jsonModel.setOperations(operations);
		
		List<JsonModel> jsonModels = new ArrayList<JsonModel>();
		jsonModels.add(jsonModel);
		jsonModels.add(jsonModel);
		jsonModels.add(jsonModel);
		
		Map<String, Object> jsonModelMap = new HashMap<String, Object>(); 
		jsonModelMap.put("map", jsonModels);
		
		
		result.put("jsonModels", jsonModels);
		result.put("jsonModelMap", jsonModelMap);
		
		String json = JSON.toJSONString(jsonModels);
		
		System.out.println(json);
		
		//结果：
		//[{"desc":"描述","operations":[{"operationDesc":"操作描述","operationName":"操作名称"},{"$ref":"$[0].operations[0]"},{"$ref":"$[0].operations[0]"},{"$ref":"$[0].operations[0]"}],"title":"json样例"},{"$ref":"$[0]"},{"$ref":"$[0]"}]
		
		/*	
		JSONArray jsonArray = JSON.parseArray();
		result.put("jsonArray", jsonArray);*/
		return result;
	}
	
}
